<!-- pay-main-list.html -->
{% extends "base.html" %}
{% block title %}
    采购管理
{% endblock %}
{% block main %}
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">采购列表</h3>
            </div>
            <div class="card-tools ml-3 mt-2">
                <a href="{% url 'pay_main_add' %}" class="btn btn-primary" style="color: white">创建采购</a>
                  <a href="{% url 'export_pay_main_csv' %}" class="btn btn-success">导出采购数据</a>
            </div>
            <div class="card-body">
                <table id="payMainTable" class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>采购编号</th>
                            <th>员工编号</th>
                            <th>采购数量</th>
                            <th>采购总价</th>
                            <th>采购时间</th>
                            <th>备注</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for pay_main in pay_mains %}
                            <tr>
                                <td>{{ pay_main.Pid }}</td>
                                <td>{{ pay_main.Eid }}</td>
                                <td>{{ pay_main.Pcount }}</td>
                                <td>{{ pay_main.Ptotal }}</td>
                                <td>{{ pay_main.Pdate }}</td>
                                <td>{{ pay_main.other }}</td>
                                <td>
                                    <a href="{% url 'pay_main_update' pay_main.Pid %}" class="btn btn-success">修改</a>
                                    <a href="{% url 'pay_detail_list' pay_main.Pid %}" class="btn btn-primary">明细管理</a>
                                    <button onclick="deletePayMain('{{ pay_main.Pid }}')" class="btn btn-danger">删除</button>
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
{% endblock %}
{% block script %}
    <script>
        $(document).ready(function () {
            $("#payMainTable").DataTable({
                "language": {
                    "url": "/static/plugins/datatables/Chinese.json"
                },
                "responsive": true,
                "autoWidth": false,
            });




        });
        const Toast = Swal.mixin({
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000
            });
         function deletePayMain(Pid) {
                Swal.fire({
                    title: '是否确认删除该采购记录?',
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonText: '确认',
                    cancelButtonText: '取消'
                }).then((result) => {
                    if (result.value) {
                        $.ajax({
                            async: true,
                            cache: false,
                            url: "/pay_main_delete/" + Pid+'/',
                            type: "delete",
                            dataType: "json",
                            success: function (res) {
                                Toast.fire({
                                    icon: 'success',
                                    title: res.message
                                });
                                setTimeout(function () {
                                    window.location.reload();
                                }, 1000);
                            },
                            error: function (res) {
                                Toast.fire({
                                    icon: 'error',
                                    title: res.message
                                });
                            }
                        });
                    }
                })
            }
    </script>
{% endblock %}
